package com.czxy.controller;

import com.czxy.domain.Teacher;
import com.czxy.service.TeacherService;
import com.czxy.util.JWTUtil;
import com.czxy.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/teacher")
public class TeacherController{

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/login")
    public BaseResult login(@RequestBody Teacher tea){
        //调用service，查找老师信息，如果找到了老师信息，就生成token，返回，否则返回错误
        Teacher dbTeacher = teacherService.login(tea);
        if (dbTeacher!=null){
            String token = JWTUtil.createToken(dbTeacher.getTeaId(), dbTeacher.getTeaName(), "teacher", 30);
            return BaseResult.ok(token);
        }
        return BaseResult.error();
    }

    /**
     * 在teacher-service中提供查找老师的方法
     * @param teaid
     * @return
     */
    @GetMapping("/{teaid}")
    public Teacher findTeacherByTeaid(@PathVariable("teaid") Integer teaid){
        Teacher tea = teacherService.getById(teaid);
        return tea;
    }
}